//
//  ClockViewController.m
//  Clock
//
//  Created by Le Thanh Hai on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ClockViewController.h"

@implementation ClockViewController

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    // background
    [NSTimer scheduledTimerWithTimeInterval:changeTheme
                                     target:self
                                   selector:@selector(updateTheme)
                                   userInfo:nil
                                    repeats:YES];
    
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:bgView]; 
    
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"character1_1.jpg"]];
    bg.frame = CGRectMake(0, 0, 320, 480);
    bg.contentMode = UIViewContentModeScaleToFill;
    [bgView addSubview:bg];
    [bg release];
    
    // content View
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:contentView];
    
    // icon link
    UIButton *btnLink = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLink setImage:[UIImage imageNamed:@"outer_link.png"] forState:UIControlStateNormal];
    [btnLink addTarget:self action:@selector(link:) forControlEvents:UIControlEventTouchUpInside];
    btnLink.frame = CGRectMake(0, 0, 60, 60);
    [self.view addSubview:btnLink];
    
    
    // setting
    settingControl = [[SettingController alloc] initWithNibName:@"SettingController" bundle:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateClock:) name:@"ClockChange" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMainMenu:) name:@"Show" object:nil];
    
    // Clock 1
    analogWhiteClock = [[AnalogClockWhiteController alloc] init];
    analogWhiteClock.view.frame = CGRectMake(10, 10, 184, 184);
    analogWhiteClock.view.backgroundColor = [UIColor clearColor];
    [contentView addSubview:analogWhiteClock.view];
    analogWhiteClock.view.hidden = YES;
    
    // Clock 2
    analogBlackClock = [[AnalogClockBlackController alloc] init];
    analogBlackClock.view.frame = CGRectMake(10, 10, 184, 184);
    analogBlackClock.view.backgroundColor = [UIColor clearColor];
    [contentView addSubview:analogBlackClock.view];
    analogBlackClock.view.hidden = YES;
    
    // Clock 3
    digitBlackClock = [[digitalBlackClock alloc] init];
    digitBlackClock.view.frame = CGRectMake(0, 0, 295, 68);  // 295
    digitBlackClock.view.backgroundColor = [UIColor clearColor];
    [contentView addSubview:digitBlackClock.view];
    digitBlackClock.view.hidden = YES;
    
    // Clock 4
    digitWhiteClock = [[digitalWhiteClock alloc] init];
    digitWhiteClock.view.frame = CGRectMake(0, 0, 295, 68);
    digitWhiteClock.view.backgroundColor = [UIColor clearColor];
    [contentView addSubview:digitWhiteClock.view];
    digitWhiteClock.view.hidden = YES;
}

- (void)link:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    
    if (![[UIApplication sharedApplication] openURL:url])        
        NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

- (void)updateTheme
{    
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"character1_%d.jpg", rand()%12 + 1]]];
    bg.frame = CGRectMake(0, 0, 320, 480);
    bg.contentMode = UIViewContentModeScaleToFill;
    [bgView addSubview:bg];
    [bg release];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([[touches anyObject] tapCount] > 1) {
        [self presentModalViewController:settingControl animated:YES];
    }
}

-(void)showMainMenu:(NSNotification *)notification
{
    [self presentModalViewController:settingControl animated:YES];
}

-(void)updateClock:(NSNotification *)notification
{
	NSLog(@"%d",[ notification.object integerValue]);
    int tmp = [ notification.object integerValue];
    if (tmp == 0) {
        digitBlackClock.view.hidden = NO;
        digitWhiteClock.view.hidden = YES;
        analogBlackClock.view.hidden = YES;
        analogWhiteClock.view.hidden = YES;
    } else if (tmp == 1) {
        digitBlackClock.view.hidden = YES;
        digitWhiteClock.view.hidden = NO;
        analogBlackClock.view.hidden = YES;
        analogWhiteClock.view.hidden = YES;
    } else if (tmp == 2) {
        digitBlackClock.view.hidden = YES;
        digitWhiteClock.view.hidden = YES;
        analogBlackClock.view.hidden = NO;
        analogWhiteClock.view.hidden = YES;
    } else if (tmp == 3) {
        digitBlackClock.view.hidden = YES;
        digitWhiteClock.view.hidden = YES;
        analogBlackClock.view.hidden = YES;
        analogWhiteClock.view.hidden = NO;
    } else if (tmp == 4) {
        digitBlackClock.view.hidden = YES;
        digitWhiteClock.view.hidden = YES;
        analogBlackClock.view.hidden = YES;
        analogWhiteClock.view.hidden = YES;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
